//
//  DMRetweetView.swift
//  DM_新浪微博
//
//  Created by Damon.JiAo on 16/3/3.
//  Copyright © 2016年 Demon_JiAo. All rights reserved.
//

import UIKit
import SnapKit


class DMRetweetView: UIView {
    
    var bottomConstrast:Constraint?
    
    /** 数据模型 */
    var retweetViewModel:DMHomeViewCellModel?{
        
        didSet{
         
            contentLabel.text = retweetViewModel?.retweetStatusString
            
           self.bottomConstrast?.uninstall()
            
            /** 判断情况进行处理转发微博配图 */
            if let pic_url = retweetViewModel?.cellModel?.retweeted_status?.pic_urls where pic_url.count > 0{
                
                retweetPictureView.hidden = false
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomConstrast = make.bottom.equalTo(retweetPictureView.snp_bottom).constraint
                })
                
                /** ???????????? */
                var result = [DMPictureViewModel]()
                for model in pic_url{
                    
                    let viewModel = DMPictureViewModel()
                    
                    viewModel.model = model
                    
                    result.append(viewModel)
                }
                
                retweetPictureView.pictureList = result
            }else{
                retweetPictureView.hidden = true
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomConstrast = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
            }
            
            
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpInfo()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /** 添加控件/设置约束 */
    private func setUpInfo(){
        
//        backgroundColor = UIColor.grayColor()
        
        addSubview(contentLabel)
        addSubview(retweetPictureView)
        
        // 设置约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(self.snp_left)
            make.top.equalTo(self.snp_top)
            make.right.equalTo(self.snp_right)
//            make.bottom.equalTo(self.snp_bottom)
            
        }
        
        retweetPictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(CellMargin)
            make.top.equalTo(contentLabel.snp_bottom)
            
//            make.size.equalTo(CGSizeMake(100, 100))
            
        }
        
        /// 转发微博的视图底部位置：
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstrast = make.bottom.equalTo(retweetPictureView.snp_bottom).constraint
        }
        
    }
    
    private lazy var contentLabel:UILabel = {
       
        let label = UILabel()
        
        label.text = "转发微博"
        
//        label.textColor = UIColor.blueColor()
        
        label.numberOfLines = 0
        
        label.font = UIFont.systemFontOfSize(14)

        
        return label
    }()
    
    lazy var retweetPictureView:DMPictureView = DMPictureView()
}
